package com.gitee.xzb617.mybatis.sqllog.spring;

import com.gitee.xzb617.mybatis.sqllog.SqlLogPlugin;
import com.gitee.xzb617.mybatis.sqllog.SqlLogProperties;
import com.gitee.xzb617.mybatis.sqllog.innerinvoke.InnerInvokeSqlLogAspectj;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

/**
 * SQL日志打印的自定装配
 * @author xuzhibin
 */
@Configuration
public class SqlLogAutoConfiguration implements WebMvcConfigurer {

    @Resource
    private SqlLogProperties sqlLogProperties;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        if (sqlLogProperties.getEnabled()) {
            registry.addInterceptor(new ReqContextInterceptor()).addPathPatterns("/**");
        }
    }

    @Bean
    @ConditionalOnBean(SqlLogProperties.class)
    public SqlLogPlugin fullSqlPrintPlugin() {
        return new SqlLogPlugin(sqlLogProperties);
    }


    @Bean
    public InnerInvokeSqlLogAspectj supportFspAspectj() {
        return new InnerInvokeSqlLogAspectj(sqlLogProperties);
    }
}
